package com.profiloc.ui.screens;

import java.util.Timer;
import java.util.TimerTask;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.MainScreen;
/*
 * @author ttbdede
 * splash screen class 
 */
public class SplashScreen extends MainScreen {
 
    Bitmap popup;
    SplashScreen screen = this;
    private Timer splashTimer = new Timer();
    private TimerTask splashTask;
    int count = 0;
    int screenWidth = Display.getWidth();
    int screenHeight = Display.getHeight();
    int yCoord;
    int xCoord;
    boolean showSplash = true;
    boolean splashDisplayed = false;

    public SplashScreen(Bitmap popup, final int seconds) {

       this.popup = resizeBitmap(popup, screenWidth, screenHeight);

        splashTask = new TimerTask() {
 
            public void run() {
                if (showSplash && !splashDisplayed) {
                    count++;
                    if (count == seconds * 10) {
                        showSplash = false;
                        splashDisplayed = true;
                        splashTimer.cancel();
                        invalidate();
                    }
                }
            }
        };
        
        splashTimer.scheduleAtFixedRate(splashTask, 100, 100);
    }
 
    protected void paint(Graphics graphics) {
    	
        super.paint(graphics);
        if (showSplash && !splashDisplayed) {
        	
            graphics.drawBitmap(xCoord, yCoord, popup.getWidth(), popup.getHeight(), popup, 0, 0);
            //draw border, delete if not needed:
            graphics.setColor(0xcccccc);
            graphics.drawRect(0, 0, popup.getWidth(), popup.getHeight());
            graphics.setColor(0x0000cd);
        }    
    }
 
    protected boolean onSavePrompt() {
		return true;
	}

    protected void onUiEngineAttached(boolean attached) {	
        showSplash = true;
        invalidate();
        super.onUiEngineAttached(attached);
    }
 
    public static Bitmap resizeBitmap(Bitmap image, int width, int height)
    {
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();
      
        // Need an array (for RGB, with the size of original image)
        int rgb[] = new int[imageWidth * imageHeight];
      
        // Get the RGB array of image into "rgb"
        image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);
      
        // Call to our function and obtain rgb2
        int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);
      
        // Create an image with that RGB array
        Bitmap temp2 = new Bitmap(width, height);
      
        temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
      
        return temp2;
    }

    private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2)
    {
        int out[] = new int[x2*y2];
        for (int yy = 0; yy < y2; yy++)
        {
            int dy = yy * y / y2;
            for (int xx = 0; xx < x2; xx++)
            {
                int dx = xx * x / x2;
                out[(x2 * yy) + xx] = ini[(x * dy) + dx];
            }
        }
        return out;
    }
}